<template>
    <show-modal :title="title" :loading="loading" width="400px" @cancel="cancel" @submit="submit">
        <a-form-model ref="form" :model="form" :rules="rules" v-bind="layout">
            <a-form-model-item label="产线名称" prop="lineName">
                <a-input v-model="form.lineName" placeholder="请输入产线名称" />
            </a-form-model-item>
            <a-form-model-item label="产线编码" prop="lineCode">
                <a-input v-model="form.lineCode" :disabled="form.lineId?true:false" placeholder="请输入产线编码" />
            </a-form-model-item>
            <a-form-model-item label="所属厂区" prop="factoryCode">
                <a-select v-model="form.factoryCode" showSearch placeholder="请选择所属厂区">
                    <a-select-option
                        :value="factory.factoryCode"
                        v-for="factory in factoryList"
                        :key="factory.factoryId">
                        {{factory.factoryName}}
                    </a-select-option>
                </a-select>
            </a-form-model-item>
            <a-form-model-item label="产线等级" prop="lineLevel">
                <a-select v-model="form.lineLevel" placeholder="请选择产线等级">
                    <a-select-option :value="level.value" v-for="(level,index) in lineLevels" :key="index">{{level.label}}</a-select-option>
                </a-select>
            </a-form-model-item>
            <a-form-model-item label="负责人" prop="lineLeader">
                <a-select v-model="form.lineLeader" showSearch optionFilterProp="name" placeholder="请选择负责人">
                    <a-select-option
                        :value="user.userId + ''"
                        :name="user.userName+user.userId"
                        v-for="user in userList"
                        :key="user.userId">
                        {{user.userName}}
                    </a-select-option>
                </a-select>
            </a-form-model-item>
            <a-form-model-item label="联系电话" prop="lineTel">
                <a-input v-model="form.lineTel" placeholder="请输入联系电话" />
            </a-form-model-item>
            <a-form-model-item label="状态">
                <a-switch :checked="form.open == 1 ? true : false" @change="onChangeShow" />
            </a-form-model-item>
        </a-form-model>

    </show-modal>
</template>

<script>
import { rules } from './rules';
import {lineLevels} from '../dictionary'
import {addInfo,updateInfo} from '@/api/system/production'

export default {
    name: 'ProductionLine',
    props:{
        info:{
            type:Object,
            default:()=>{}
        },
        factoryList:{
            type:Array,
            default:()=>[]
        },
        userList:{
            type:Array,
            default:()=>[]
        }
    },
    data() {
        return {
            loading: false,
            title: '产线信息',
            rules,
            lineLevels,
            form: {
                lineName: '',
                lineCode:'',
                factoryCode:undefined,
                lineLevel:undefined,
                lineLeader:undefined,
                lineTel:'',
                open:1
            },
            layout: {
                labelCol: { span: 5 },
                wrapperCol: { span: 19 }
            },
        };
    },
    created() {
        this.form = {...this.form,...this.info}
    },
    methods: {
        //提交信息
        submit(){
            this.$refs.form.validate(async valid => {
                if (valid) {
                    try {
                        this.loading = true;
                        let form = this.form;
                        let res = null
                        if (form.lineId) {
                            //编辑
                            res = await updateInfo(form);
                        } else {
                            //新增addInfo
                            res = await addInfo(form);
                        }
                        let {code} = res;
                        if(code==200){
                            this.$message.success('操作成功!');
                            this.$emit('submit');
                        }
                    } finally {
                        this.loading = false;
                    }
                }
            });
        },
        //切换状态
        onChangeShow(checked) {
            if (checked) {
                this.form.open = 1;
            } else {
                this.form.open = 0;
            }
        },
       
        //取消
        cancel() {
            this.$emit('cancel');
        },
        
    }
};
</script>

<style lang="scss" scoped></style>
